//////////////////////////////////// Ajax //////////////////////////////////////////////////////////////
function deleteProduct(id) {

	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp = new XMLHttpRequest();
	} else {// code for IE6, IE5
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}

	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			if (xmlhttp.responseText != "ERROR") {
				document.getElementById("div_" + id).style.display = 'none';

				if (xmlhttp.responseText > 0)
					document.getElementById("b_cart").innerHTML = 'Aveti ' + xmlhttp.responseText + ' produse in cos';
				else
					document.getElementById("div_cart").innerHTML = '<br/><br/><div id="div_cart_empty"><img src="img/site/cart_empty.gif" alt="?" /> <b>Cosul este gol</b></div>';
			} else {
				var label = document.getElementById("lbError_" + id);
				if (label)
					label.innerHTML = 'S-a produs o eroare la stergerea produsului din cos';
				else {
					label = document.createElement('label');
					label.setAttribute('id', 'lbError_' + id);
					label.setAttribute('for', 'quantity_' + id);
					label.setAttribute('generated', 'true');
					label.setAttribute('class', 'error');

					label.innerHTML = 'S-a produs o eroare la stergerea produsului din cos';
					document.getElementById("div_" + id).appendChild(
							label);
				}
			}

		}
	}
	xmlhttp.open("GET", "cart?action=delete&id=" + id,
			true);
	xmlhttp.send();
}

function UpdateProductQuantity(id) {

	if (CheckQuantity(id) != true)
		return;

	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp = new XMLHttpRequest();
	} else {// code for IE6, IE5
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}

	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			if (xmlhttp.responseText == "OK") {

				var label = document.getElementById("lbError_" + id);
				if (label)
					label.innerHTML = 'Cantitatea a fost updatata';
				else {
					label = document.createElement('label');
					label.setAttribute('id', 'lbError_' + id);
					label.setAttribute('for', 'quantity_' + id);
					label.setAttribute('generated', 'true');
					label.setAttribute('class', 'error');

					label.innerHTML = 'Cantitatea a fost updatata';
					document.getElementById("div_quantity_" + id).appendChild(
							label);
				}

				// se updateaza cantitatea subtotalului
				var div = document.getElementById("div_sub_" + id);
				var quantity = document.getElementById("quantity_" + id).value;
				var price = document.getElementById("price_" + id).innerHTML;
				div.innerHTML = '<b>Subtotal: ' + quantity * price+'<b>';
			}
		}
	}

	xmlhttp.open("GET", "cart?action=edit&id=" + id
			+ "&quantity=" + document.getElementById("quantity_" + id).value,
			true);
	xmlhttp.send();
}

// //////////////////// validation ////
// ////////////////////////////////////////////////////////////////////
function CheckQuantity(id) {
	try {
		var quantity = document.getElementById("quantity_" + id).value;

		if (quantity == '')
			throw 'Introduceti cantitatea';
		else if (isNaN(quantity))
			throw 'Introduceti numai cifre';
		else if (quantity <= 0)
			throw 'Cantitatea trebuie sa fie pozitiva';

		var label = document.getElementById("lbError_" + id);
		if (label)
			document.getElementById("div_quantity_" + id).removeChild(label);

		return true;

	} catch (err) {

		var label = document.getElementById("lbError_" + id);
		if (label)
			label.innerHTML = err;
		else {
			label = document.createElement('label');
			label.setAttribute('id', 'lbError_' + id);
			label.setAttribute('for', 'quantity_' + id);
			label.setAttribute('generated', 'true');
			label.setAttribute('class', 'error');

			label.innerHTML = err;
			document.getElementById("div_quantity_" + id).appendChild(label);
		}
	}
}
